import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import layout from "@/views/users/user";
import layouts from '@/views/articles/article';
//静态路由表
const routes = [
  // 登陆
  {
    path: '/',
    name: 'Login',
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/profile',
    name: 'profile',
    redirect: "/user",
    component: () => import('@/views/list/Profile.vue'),
    children: [
      {
        path: '/user',
        component: layout,
        name: 'userManage',
        meta: {
          title: 'user',
          icon: 'personnel'
        },
      },
      {
        path: '/user/manage',
        component: () => import('@/views/users/manage'),
        meta: {
          title: 'userManage',
          icon: 'personnel-manage'
        }
      },
      {
        path: '/user/role',
        component: () => import('@/views/users/role'),
        name: 'roleList',
        meta: {
          title: 'roleList',
          icon: 'role'
        }
      },
      {
        path: '/user/permission',
        component: () => import('@/views/users/permission'),
        name: 'permissionList',
        meta: {
          title: 'permissionList',
          icon: 'permission'
        }
      },
      {
        path: '/user/info/:id',
        name: 'userInfo',
        component: () => import('@/views/users/info'),
        meta: {
          title: 'userInfo'
        }
      },
      {
        path: '/user/import',
        name: 'import',
        component: () => import('@/views/users/import'),
        meta: {
          title: 'excelImport'
        }
      },

      {
        path: '/article',
        component: layouts,
        name: 'articleRanking',
        meta: {
          title: 'article',
          icon: 'article'
        },
      },
      {
        path: '/article/ranking',
        component: () => import('@/views/articles/ranking'),
        meta: {
          title: 'articleRanking',
          icon: 'article-ranking'
        }
      },
      {
        path: '/article/:id',
        component: () => import('@/views/articles/detail'),
        meta: {
          title: 'articleDetail'
        }
      },
      {
        path: '/article/create',
        component: () => import('@/views/articles/create'),
        name: 'articleCreate',
        meta: {
          title: 'articleCreate',
          icon: 'article-create'
        }
      },
      {
        path: '/article/editor/:id',
        component: () => import('@/views/articles/editor'),
        meta: {
          title: 'articleEditor'
        }
      },
      {
        path: '/chart',
        component: () => import('@/views/Chart'),
        meta: {
          title: 'Chart'
        }
      },
    ]
  },
  //以下为测试页面
  {
    path: '/about',
    name: 'About',
    component: () => import(/* webpackChunkName: "About" */ '../views/About.vue')
  },
  {
    path: '/test',//vue3基本语法演示
    name: 'Test',
    component: () => import(/* webpackChunkName: "Test" */ '../views/Test.vue')
  },

  //找不到页面到404,！！注意vue3里不能用*
  // {
  //  path: '/:catchAll(.*)',
  //  redirect: '/404'
  // }
]

const router = createRouter({
  history: createWebHashHistory(), // hash模式：createWebHashHistory，history模式：createWebHistory
  routes
})
const whiteList = ['/test'];

// router.beforeEach((to, from, next) => {
//   let token = localStorage.getItem("rystore")
//   if (token) {
//     next()
//   } else {
//     if (whiteList.indexOf(to.path) != -1) {
//       next()
//     }
//     else {
//       next("/")
//     }
//   }
// })

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("rystore")
  const isLogin = (to.path === '/')
  if (token && isLogin) {
    return next()
  }
  // 如果没有token，并不是在登录页，跳转到登录页
  if (!token && !isLogin && whiteList.indexOf(to.path) == 1) {
    return next({ name: 'Login' })
  }
  next()
})



export default router
